package com.cskaoyan.mall.service;

import com.cskaoyan.mall.dao.AdminDao;
import com.cskaoyan.mall.dao.AdminDaoImpl;
import com.cskaoyan.mall.model.admin.Admin;
import com.cskaoyan.mall.model.admin.AdminLoginBO;
import com.cskaoyan.mall.model.admin.AdminVO;
import com.cskaoyan.mall.model.ChangePwd;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: jyf2000jyf
 */
public class AdminServiceImpl implements AdminService {
    private AdminDao adminDao = new AdminDaoImpl();

    @Override
    public int login(AdminLoginBO adminLoginBO) {
        //login的参数传递AdminLoginBO还是传递Admin
        //传递Admin今后的适配性更好一些，因为Admin就是和数据库表字段一一对应
        //loginBo，可能在另外一个接口中，传递过来的不是loginBo
        Admin admin = new Admin(null, adminLoginBO.getEmail(), null, adminLoginBO.getPwd());
        return adminDao.login(admin);
    }

    @Override
    public List<AdminVO> getAllAdmins() {
        List<AdminVO> adminVOList = new ArrayList<>();
        List<Admin> adminList = adminDao.getAllAdmins();
        for (Admin admin : adminList) {
            adminVOList.add(new AdminVO(admin));
        }
        return adminVOList;
    }

    @Override
    public boolean deleteAdmins(int id) {
        return adminDao.deleteAdmins(id);
    }

    @Override
    public AdminVO getAdminsInfo(int id) {
        Admin admin = adminDao.getAdminsInfo(id);
        if(admin == null){
            return null;
        }
        return new AdminVO(admin);
    }

    @Override
    public boolean updateAdminss(AdminVO adminVO) {
        return adminDao.updateAdminss(new Admin(adminVO));
    }

    @Override
    public List<AdminVO> getSearchAdmins(AdminVO adminVO) {
        Admin inputAdmin = new Admin(null, adminVO.getEmail(), adminVO.getNickname(), null);
        List<Admin> adminList = adminDao.getSearchAdmins(inputAdmin);
        List<AdminVO> adminVOList = new ArrayList<>();
        for (Admin admin : adminList) {
            adminVOList.add(new AdminVO(admin));
        }
        return adminVOList;
    }

    @Override
    public boolean addAdminss(AdminVO adminVO) {
        Admin Admin = new Admin(null,adminVO.getEmail(),adminVO.getNickname(),adminVO.getPwd());
        return adminDao.addAdminss(Admin);
    }

    @Override
    public String changePwd(ChangePwd pwd) {
        if(pwd.getNewPwd().isEmpty() || pwd.getNewPwd() == null || pwd.getConfirmPwd().isEmpty() || pwd.getConfirmPwd() == null){
            return null;
        }
        if(!pwd.getNewPwd().equals(pwd.getConfirmPwd())){
            // 不会造成冲突，不合理的密码
            return "确认密码和新密码不一致";
        }
        return adminDao.changePwd(pwd);
    }
}
